package com.nyt.report.config;

import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import javax.cache.CacheManager;
import java.io.FileNotFoundException;
import java.net.URI;

@Configuration
@EnableCaching
public class EhcacheConfig {

    @Value("${ehcache.path}")
    String EHCACHE_PATH;

    @Bean
    public JCacheCacheManager cacheManager() throws FileNotFoundException {
        return new JCacheCacheManager(jCacheManager());
    }

    @Bean
    public CacheManager jCacheManager() throws FileNotFoundException {
        EhcacheCachingProvider provider = new EhcacheCachingProvider();
        URI uri = ResourceUtils.getFile(EHCACHE_PATH).toURI();
        CacheManager cacheManager = provider.getCacheManager(uri, getClass().getClassLoader());
        // 指定缓存名称再返回
        //return cacheManager.getCache("")；
        return cacheManager;
    }
}
